import java.io.*;
import java.awt.image.BufferedImage;

import java.awt.*;

/*
 * Avatar.java
 *
 * Created on 4. november 2007, 17:24
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
import java.net.URI;
import java.util.ArrayList;

/**
 *
 * @author Dark_Wolf
 */
public class Mp3Avatar extends Avatar  {
    
    
   
    private File mp3;
    
    public Mp3Avatar(File file,URI folder, String artist,String album,CDShelf shelf) {
        mp3 = file;
        this.folder = folder;
        this.artist=artist;
        this.album=album;
      
        
        this.shelf = shelf;
        fx = CrystalCaseFactory.getInstance();
        gotImage = true;
        
    }
 
   
    
    public boolean loadImage() {
        
        if (!loaded) {
           
                image = fx.createReflectedPicture(ImageTagger.readImage(mp3));
                loaded = true;
            
            }
        
        return !loaded;
    }
     public void embedImage() {
        ArrayList<String> files = SqliteInterface.getInstance().getURIsForAlbum(album);
        File imageFile = ImageTagger.getImageFile(mp3);
        for (String filePath : files) {
            System.out.println(filePath);
            if (filePath.substring(filePath.lastIndexOf(".")+1).equalsIgnoreCase("mp3")){
                File tempFile= AlbumHelper.songbirdToJavaFile(filePath);
                System.out.println("Mp3 found" + tempFile.isFile());
                ImageTagger.deleteImage(tempFile);
                ImageTagger.writeImage(imageFile,tempFile);
            }
            
        }
    }
   
   public void setImage(File newFile){
        String ending = newFile.getName().substring(newFile.getName().lastIndexOf(".")+1);
        System.out.println("Ending is: " +  ending);
        System.out.println("Folder is: " +  folder.getPath());
        File imageFile = AlbumHelper.songbirdToJavaFile(folder.getRawPath(), artist + "-" + album, ending);
        System.out.println("file is: "  + imageFile);
        AlbumHelper.fileCopy(newFile, imageFile);
                
        if (shelf.getWriteMp3()){
                embedImage();      
        }
        if (loaded){
            unloadImage();
            loadImage();
        }
    }
     
    
    
    public BufferedImage getRawImage(){
        
                return ImageTagger.readImage(new File(fileURI));
           
            
    }
   
    
}



